package utils;

import java.io.File;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public final class StringUtil {
	private static final String EMAIL_REGEX = "^[a-zA-Z0-9\\._-]{1,64}@[A-Za-z0-9]{2,64}(\\.[A-Za-z0-9]{2,64})*(\\.[A-Za-z]{2,4})$";
	private static final String[] htmlChar = new String[] { "&", "<", ">", "'",
			"\"" };
	private static final String[] htmlNameCode = new String[] { "&amp;",
			"&lt;", "&gt;", "&apos;", "&quot;" };

	public static boolean validateEmail(String emailAdress) {
		if (StringUtil.isNullOrEmpty(emailAdress))
			return false;
		return emailAdress.trim().matches(EMAIL_REGEX);
	}

	public static boolean isNullOrEmpty(final String s) {
		return (s == null || s.trim().length() == 0);
	}

	public static boolean isNullOrEmptyNotTrim(final String s) {
		return (s == null || s.length() == 0);
	}

	public static String fromNullToEmtpyString(String a) {
		if (a == null) {
			return "";
		} else {
			return a;
		}
	}

	public static String toStringNumberMMYYY(String str) {
		String strMM = str.split("/")[0];
		String strYYYY = str.split("/")[1];
		strMM = Integer.valueOf(strMM) < 10 ? "0" + strMM : strMM;
		return strMM + "/" + strYYYY;
	}

	public static String convertConvfact(Integer quality, Integer convfact) {
		int first = quality / convfact;
		int last = quality % convfact;
		return first + "/" + last;
	}

	public static String CodeAddName(String code, String name) {
		if (StringUtil.isNullOrEmpty(code) && !StringUtil.isNullOrEmpty(name)) {
			return name;
		}
		if (!StringUtil.isNullOrEmpty(code) && StringUtil.isNullOrEmpty(name)) {
			return code;
		}
		if (!StringUtil.isNullOrEmpty(code) && !StringUtil.isNullOrEmpty(name)) {
			return code + " - " + name;
		}
		return "";
	}

	public static String CodeAddNameEx(String code, String name) {
		if (StringUtil.isNullOrEmpty(code) && !StringUtil.isNullOrEmpty(name)) {
			return name;
		}
		if (!StringUtil.isNullOrEmpty(code) && StringUtil.isNullOrEmpty(name)) {
			return code;
		}
		if (!StringUtil.isNullOrEmpty(code) && !StringUtil.isNullOrEmpty(name)) {
			return code + "," + name;
		}
		return "";
	}

	public static String priceWithDecimal(Double price) {
		DecimalFormat formatter = new DecimalFormat("###,###,###.00");
		return formatter.format(price);
	}

	public static String priceWithoutDecimal(Double price) {
		DecimalFormat formatter = new DecimalFormat("###,###,###.##");
		return formatter.format(price);
	}

	public static String priceToString(Double price) {
		String toShow = priceWithoutDecimal(price);
		if (toShow.indexOf(".") > 0) {
			return priceWithDecimal(price);
		} else {
			return priceWithoutDecimal(price);
		}
	}

	public static String convertStringToHTMLCode(String strSource) {
		if (isNullOrEmpty(strSource)) {
			return "";
		}
		for (int i = 0; i < htmlChar.length; i++) {
			strSource = strSource.replaceAll(htmlChar[i], htmlNameCode[i]);
		}
		return strSource;
	}

	public static String replaceSeparatorChar(String fileName) {
		return fileName.replace('/', File.separatorChar);
	}

	public static String convertHTMLNewLine(String str) {
		String result = "";
		String regex = "(\r\n|\n)";
		String replacement = "&newline;";
		result = str.replaceAll(regex, replacement);
		return result;
	}

	public static String convertHTMLBreakLine(String str) {
		String result = "";
		String regex = "(\r\n|\n)";
		String replacement = "<br />";
		result = str.replaceAll(regex, replacement);
		return result;
	}

	public static String replaceParam(String param) {
		if (!StringUtil.isNullOrEmpty(param)) {
			param = param.replace(",", "").trim();
		}
		return param;
	}
	
	public static String toOracleSearchLike(String searchText) {
		String escapeChar = "/";
		String[] arrSpPat = {"/", "%", "_", "\\\\"};
		
		for (String str: arrSpPat) {
			if (!StringUtil.isNullOrEmpty(searchText)) {
				searchText = searchText.replaceAll(str, escapeChar + str);
			}
		}
		searchText = "%" + searchText + "%";
		return searchText;
	}
}
